package second.array;

public class Solution06 {
    public int[][] generateMatrix(int n) {
        int row1 = 0;
        int row2 = n - 1;
        int col1 = 0;
        int col2 = n - 1;
        int[][] res = new int[n][n];
        int num = 1;
        while (row1 < n / 2) {
            for (int i = col1; i < col2; i++) {
                res[row1][i] = num++;
            }
            for (int i = row1; i < row2; i++) {
                res[i][col2] = num++;
            }
            for (int i = col2; i > col1; i--) {
                res[row2][i] = num++;
            }
            for (int i = row2; i > row1; i--) {
                res[i][col1] = num++;
            }
            row1++;
            row2--;
            col1++;
            col2--;
        }
        if (n % 2 == 1) res[row1][row1] = num;
        return res;
    }
}
